package cn.iocoder.yudao.module.ent.controller.admin.receivedata;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.ent.controller.admin.receivedata.vo.ReceiveDataPageReqVO;
import cn.iocoder.yudao.module.ent.controller.admin.receivedata.vo.ReceiveDataRespVO;
import cn.iocoder.yudao.module.ent.controller.admin.receivedata.vo.ReceiveDataSaveReqVO;
import cn.iocoder.yudao.module.ent.dal.dataobject.receivedata.ReceiveDataDO;
import cn.iocoder.yudao.module.ent.service.receivedata.ReceiveDataService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "企业大数据 - 领取列表")
@RestController
@RequestMapping("/ent/receive-data")
@Validated
public class ReceiveDataController {

    @Resource
    private ReceiveDataService receiveDataService;

//    @Resource
//    private AdminUserApi adminUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建领取列表")
    @PreAuthorize("@ss.hasPermission('ent:receive-data:create')")
    public CommonResult<Long> createReceiveData(@Valid @RequestBody ReceiveDataSaveReqVO createReqVO) {
        return success(receiveDataService.createReceiveData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新领取列表")
    @PreAuthorize("@ss.hasPermission('ent:receive-data:update')")
    public CommonResult<Boolean> updateReceiveData(@Valid @RequestBody ReceiveDataSaveReqVO updateReqVO) {
        receiveDataService.updateReceiveData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除领取列表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ent:receive-data:delete')")
    public CommonResult<Boolean> deleteReceiveData(@RequestParam("id") Long id) {
        receiveDataService.deleteReceiveData(id);
        return success(true);
    }

    @DeleteMapping("/batch-delete")
    @Operation(summary = "删除领取列表")
    @Parameter(name = "idList", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ent:receive-data:delete')")
    public CommonResult<Boolean> deleteReceiveData(@RequestParam("idList") List<Long> idList) {
        receiveDataService.batchDeleteReceiveData(idList);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得领取列表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ent:receive-data:query')")
    public CommonResult<ReceiveDataRespVO> getReceiveData(@RequestParam("id") Long id) {
        ReceiveDataDO receiveData = receiveDataService.getReceiveData(id);
        return success(BeanUtils.toBean(receiveData, ReceiveDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得领取列表分页")
    @PreAuthorize("@ss.hasPermission('ent:receive-data:query')")
    public CommonResult<PageResult<ReceiveDataRespVO>> getReceiveDataPage(@Valid ReceiveDataPageReqVO pageReqVO) {
        pageReqVO.setOwnerUserId(SecurityFrameworkUtils.getLoginUserId());
        PageResult<ReceiveDataDO> pageResult = receiveDataService.getReceiveDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReceiveDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出领取列表 Excel")
    @PreAuthorize("@ss.hasPermission('ent:receive-data:export')")
    @OperateLog(type = EXPORT)
    public void exportReceiveDataExcel(@Valid ReceiveDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReceiveDataDO> list = receiveDataService.getReceiveDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "领取列表.xls", "数据", ReceiveDataRespVO.class,
                        BeanUtils.toBean(list, ReceiveDataRespVO.class));
    }

}